﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Hangjing.Weixin;
using Hangjing.Model;
using Hangjing.SQLServerDAL;
using Hangjing.Common;

namespace Html5.Account
{
    /// <summary>
    /// 微信授权回调界面
    /// </summary>
    public partial class shopauthcallbak : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string returnurl = WebUtility.FixgetCookie("reurl");
            if (Request["code"] != null)
            {
                string code = Request["code"];
                WebOAuth wo = new WebOAuth(Context, code);

                string openid = "";

                string state = HjNetHelper.GetQueryString("state");
                string[] paramdatas = state.Split('_');

                weixinUserInfo user = WebOAuth.getWeixinUserInfo(code, Convert.ToInt32(paramdatas[0]));
                if (user != null)
                {
                    openid = user.openid;
                    Hangjing.DBUtility.SQLHelper.excutesql("UPDATE dbo.points SET PosAddr = '" + openid + "',PosRoom = '"+user.nickname+"' WHERE unid = " + paramdatas[1]);
                }

                WebUtility.FixsetCookie("shopopenid", openid, 365);
                HJlog.toLog("state=" + state + "\r\nreturnurl=" + returnurl);
                if (returnurl != null && returnurl != "")
                {
                    if (returnurl.IndexOf("?") > 0)
                    {
                        returnurl += "&openid=" + openid;
                    }
                    else
                    {
                        returnurl += "?openid=" + openid;
                    }

                    Response.Redirect(returnurl);

                }
                else
                {
                    Response.Redirect("/shop/index.aspx");
                }


            }
            else
            {
                Response.Write("授权失败,请重试");
            }

        }
    }
}